﻿/*
=======================================================
    CheckWirerope【在用绳检测】
=======================================================
*/


using StackExchange.Redis;

namespace Services
{
    public interface ICheckWireropeLogic : ILogic<CheckWireropeModel, CheckWireropeFilterModel>
    {
        bool Check(CheckWireropeModel model);
        CheckWireropeModel GetPrevCheckWireropeModel(CheckWireropeFilterModel filterModel);
    }


    public class CheckWireropeLogic : BaseLogic<CheckWireropeModel, CheckWireropeFilterModel, CheckWirerope, ICheckWireropeRepository, ICheckWireropeMapping, ICheckWireropeService>, ICheckWireropeLogic
    {
        #region Ioc
        private readonly ICheckWireropeDetailRepository _ICheckWireropeDetailRepository;
        private readonly ICheckWireropeDetailMapping _ICheckWireropeDetailMapping;

        private readonly ICheckWireropeDetailDataRepository _ICheckWireropeDetailDataRepository;
        private readonly ICheckWireropeDetailDataMapping _ICheckWireropeDetailDataMapping;

        private readonly ICheckWireropeDetailPositionRepository _ICheckWireropeDetailPositionRepository;
        private readonly ICheckWireropeDetailPositionMapping _ICheckWireropeDetailPositionMapping;

        private readonly IUsingWireropeRepository _UsingWireropeRepository;
        private readonly IUsingWireropeMapping _UsingWireropeMapping;

        private readonly IJudgeWireropeStatusMonitor _IJudgeWireropeStatusMonitor;

        private readonly ICreateCode _ICreateCode;

        public CheckWireropeLogic(ICurrentUser currentUser, ICheckWireropeRepository repository, ICheckWireropeMapping mapping, ICheckWireropeService service,
            ICheckWireropeDetailRepository __ICheckWireropeDetailRepository,
            ICheckWireropeDetailMapping __ICheckWireropeDetailMapping,
            ICheckWireropeDetailDataRepository __ICheckWireropeDetailDataRepository,
            ICheckWireropeDetailDataMapping __ICheckWireropeDetailDataMapping,
            ICheckWireropeDetailPositionRepository __ICheckWireropeDetailPositionRepository,
            ICheckWireropeDetailPositionMapping __ICheckWireropeDetailPositionMapping, IUsingWireropeRepository usingWireropeRepository, IUsingWireropeMapping usingWireropeMapping, IJudgeWireropeStatusMonitor iJudgeWireropeStatusMonitor, ICreateCode iCreateCode)
            : base(currentUser, repository, mapping, service)
        {
            _ICheckWireropeDetailRepository = __ICheckWireropeDetailRepository;
            _ICheckWireropeDetailMapping = __ICheckWireropeDetailMapping;
            _ICheckWireropeDetailDataRepository = __ICheckWireropeDetailDataRepository;
            _ICheckWireropeDetailDataMapping = __ICheckWireropeDetailDataMapping;
            _ICheckWireropeDetailPositionRepository = __ICheckWireropeDetailPositionRepository;
            _ICheckWireropeDetailPositionMapping = __ICheckWireropeDetailPositionMapping;
            _UsingWireropeRepository = usingWireropeRepository;
            _UsingWireropeMapping = usingWireropeMapping;
            _IJudgeWireropeStatusMonitor = iJudgeWireropeStatusMonitor;
            _ICreateCode = iCreateCode;
        }
        #endregion

        #region Insert
        public override CheckWireropeModel Insert(CheckWireropeModel model)
        {
            UsingWirerope usingWirerope = null;
            model.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            model.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            model.GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID;
            model.Code = _ICreateCode.GetCheckWireropeCode();

            //model.Status = (int)CheckWirerope_Status.未提交; 前端给状态值，是保存还是提交
            if (model.Status == (int)CheckWirerope_Status.已检测)
            {
                model.SubmitUserID = _currentUser.CurrentUserInfo.UserID;
                model.SubmitUserName = _currentUser.CurrentUserInfo.UserName;
                model.SubmitDate = DateTime.Now;

                if (model.NextCheckDate.HasValue)
                {
                    usingWirerope = _UsingWireropeRepository.GetByID(model.UsingWireropeID);
                    usingWirerope.NextCheckDate = model.NextCheckDate.Value;
                }
            }


            model.CheckWireropeID = _repository.InsertForAdmin(_mapping.Model2Entity(model), _ICheckWireropeDetailMapping.Model2Entity(model.CheckWireropeDetailModels), usingWirerope);

            //需要增加灯状态的数据，重新判断灯的状态，并且判断是否增加或者修改
            if (model.Status == (int)CheckWirerope_Status.已检测)
            {
                var tempInfo = _IJudgeWireropeStatusMonitor.JudgeWireropeMonitor(model.UsingWireropeID);

                _IJudgeWireropeStatusMonitor.InsertOrUpdateWireropeMonitor(tempInfo);
            }

            return model;
        }
        #endregion

        #region Update
        public override int Update(CheckWireropeModel model)
        {
            UsingWirerope usingWirerope = null;

            if (model.Status == (int)CheckWirerope_Status.已检测)
            {
                model.SubmitUserID = _currentUser.CurrentUserInfo.UserID;
                model.SubmitUserName = _currentUser.CurrentUserInfo.UserName;
                model.SubmitDate = DateTime.Now;

                if (model.NextCheckDate.HasValue)
                {
                    usingWirerope = _UsingWireropeRepository.GetByID(model.UsingWireropeID);
                    usingWirerope.NextCheckDate = model.NextCheckDate.Value;
                }
            }

            _repository.UpdateForAdmin(_mapping.Model2Entity(model), usingWirerope);

            //需要增加灯状态的数据，重新判断灯的状态，并且判断是否增加或者修改
            if (model.Status == (int)CheckWirerope_Status.已检测)
            {
                var tempInfo = _IJudgeWireropeStatusMonitor.JudgeWireropeMonitor(model.UsingWireropeID);

                _IJudgeWireropeStatusMonitor.InsertOrUpdateWireropeMonitor(tempInfo);
            }
            return 1;
        }
        #endregion

        #region GetByID
        public override CheckWireropeModel GetByID(int id)
        {
            var model = GetModel(new CheckWireropeFilterModel { _CheckWireropeID = id });

            var list = _ICheckWireropeDetailRepository.GetListByCheckWireropeID(id);

            var detailIDs = list.Select(x => x.CheckWireropeDetailID).ToList();

            var cwdds = _ICheckWireropeDetailDataRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());
            var cwdps = _ICheckWireropeDetailPositionRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());

            foreach (var item in list)
            {
                item.CheckWireropeDetailDatas = cwdds.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
                item.CheckWireropeDetailPositions = cwdps.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
            }

            model.CheckWireropeDetailModels = _ICheckWireropeDetailMapping.Entity2Model(list);


            return model;
        }
        #endregion

        #region GetPage
        public override PageModel<CheckWireropeModel> GetPage(CheckWireropeFilterModel filterModel, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            PageModel<CheckWireropeModel> pageModel = new PageModel<CheckWireropeModel>();

            var (items, totalCount) = _repository.GetPage(_mapping.FilterModel2Where(filterModel), pageIndex, pageSize, orderBy);
            pageModel.TotalCount = totalCount;
            var ItemsTemp = _mapping.Entity2Model(items);

            var ids = ItemsTemp.Select(x => x.UsingWireropeID).ToList();

            var where = Expressionable.Create<UsingWirerope>();
            where = where.And(x => ids.Contains(x.UsingWireropeID));
            var list = _UsingWireropeMapping.Entity2Model(_UsingWireropeRepository.GetSearch(where));

            foreach (var item in ItemsTemp)
            {
                item.UsingWireropeModel = list.FirstOrDefault(x => x.UsingWireropeID == item.UsingWireropeID);
            }

            pageModel.Items = ItemsTemp;


            return pageModel;
        }
        #endregion

        #region Check
        public bool Check(CheckWireropeModel model)
        {
            UsingWirerope usingWirerope = null;

            if (model.Status == (int)CheckWirerope_Status.已检测)
            {
                model.SubmitUserID = _currentUser.CurrentUserInfo.UserID;
                model.SubmitUserName = _currentUser.CurrentUserInfo.UserName;
                model.SubmitDate = DateTime.Now;

                if (model.NextCheckDate.HasValue)
                {
                    usingWirerope = _UsingWireropeRepository.GetByID(model.UsingWireropeID);
                    usingWirerope.NextCheckDate = model.NextCheckDate.Value;
                }
            }

            _repository.Check(_mapping.Model2Entity(model), usingWirerope);

            //需要增加灯状态的数据，重新判断灯的状态，并且判断是否增加或者修改
            if (model.Status == (int)CheckWirerope_Status.已检测)
            {
                var tempInfo = _IJudgeWireropeStatusMonitor.JudgeWireropeMonitor(model.UsingWireropeID);

                _IJudgeWireropeStatusMonitor.InsertOrUpdateWireropeMonitor(tempInfo);
            }

            return true;
        }
        #endregion

        public override CheckWireropeModel GetModel(CheckWireropeFilterModel filterModel, string orderBy = null)
        {
            var model = base.GetModel(filterModel, orderBy);
            if (model.IsEmpty()) return null;

            int id = model.CheckWireropeID;
            var list = _ICheckWireropeDetailRepository.GetListByCheckWireropeID(id);

            var detailIDs = list.Select(x => x.CheckWireropeDetailID).ToList();

            var cwdds = _ICheckWireropeDetailDataRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());
            var cwdps = _ICheckWireropeDetailPositionRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());

            foreach (var item in list)
            {
                item.CheckWireropeDetailDatas = cwdds.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
                item.CheckWireropeDetailPositions = cwdps.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
            }

            model.CheckWireropeDetailModels = _ICheckWireropeDetailMapping.Entity2Model(list);


            return model;
        }

        #region 获取上次检测数据
        public CheckWireropeModel GetPrevCheckWireropeModel(CheckWireropeFilterModel filterModel)
        {
            var model = _mapping.Entity2Model(_repository.GetPrevCheckWirerope(filterModel._UsingWireropeID.Value, filterModel._CreateDate));
            if (model.IsEmpty()) return null;

            int id = model.CheckWireropeID;
            var list = _ICheckWireropeDetailRepository.GetListByCheckWireropeID(id);

            var detailIDs = list.Select(x => x.CheckWireropeDetailID).ToList();

            var cwdds = _ICheckWireropeDetailDataRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());
            var cwdps = _ICheckWireropeDetailPositionRepository.GetListByCheckWireropeDetailIDs(detailIDs.ToArray());

            foreach (var item in list)
            {
                item.CheckWireropeDetailDatas = cwdds.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
                item.CheckWireropeDetailPositions = cwdps.Where(x => x.CheckWireropeDetailID == item.CheckWireropeDetailID).ToList();
            }

            model.CheckWireropeDetailModels = _ICheckWireropeDetailMapping.Entity2Model(list);


            return model;
        } 
        #endregion
    }
}
